package com.primary.controller.check;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Shen Jianhui
 * @time 2023/08/05 18:01 Saturday
 * @description Actually, someone may use other tools but not browser to use the interface,
 *              in order to avoid to make some incorrect data will be inserted, it's necessary to check data,
 *              including illegal data or some data that may lead the System Error
 * @return The result of checking about the username and password
 */
public class NameAndPasswordCheck {

    /******************************** Checking the username START   **************************************/
    public static boolean checkUserName(String userName) {
        // In the username, a user can insert its information about username include numbers,letters and underline,
        // at the same time, the length of username will be limited 1 to 12 characters
        Pattern pattern = Pattern.compile("(^\\w{1,12}$)");
        Matcher matcher = pattern.matcher(userName);
        return matcher.matches();
    }

    /******************************* Checking the username   END   **********************************/


    /******************************* Checking the password   START **********************************/
    /**
     * 假定设置密码时，密码规则为：  字母、数字、特殊符号，至少匹配2种
     * 则密码可能出现的情况有：
     * 1、数字+特殊符号
     * 2、字母+特殊符号
     * 3、字母+数字
     * 4、字母+数字+特殊符号
     * (组合与顺序无关)
     * 解决思路：
     * 1、遍历字符串的字符数组，查看是否包含目标特殊字符，若包含，则标记字符串
     * 包含特殊字符，并替换当前特殊字符为''。
     * 2、判断剩下的字符组成的字符串，是否匹配以下情况
     * - 纯字母
     * - 纯数字
     * - 字母+数字
     * 3、字符串匹配规则
     * 纯字母+包含特殊字符  ---- 匹配通过
     * 纯数字+包含特殊字符 ---- 匹配通过
     * 字母+数字+包含个数字符 ---- 匹配通过
     */
    //特殊字符
    public static final String SPEC_CHARACTERS = "!\"#$%&'()*+,-./:;<=>?@\\]\\[^_`{|}~";
    // 纯字母
    public static final String character = "[a-zA-Z]{1,}$";
    // 纯数字
    public static final String numberic = "[0-9]{1,}$";
    // 字母和数字
    public static final String number_and_character = "((^[a-zA-Z]{1,}[0-9]{1,}[a-zA-Z0-9]*)+)" +
            "|((^[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]*)+)$";
    // 字母或数字
    public static final String number_or_character = "[a-zA-Z0-9]+$";
    // 字母数字下划线
    public static final String number_or_character_or_underline = "\\w+$";

    public static boolean checkPassword(String targetString) {
        if (targetString.length() <= 8 || targetString.length() >= 16) {
            return false;
        }
        String opStr = targetString;
        boolean isLegal = false;
        boolean hasSpecChar = false;
        char[] charArray = opStr.toCharArray();
        for (char c : charArray) {
            if (SPEC_CHARACTERS.contains(String.valueOf(c))) {
                hasSpecChar = true;
                // 替换此字符串
                opStr = opStr.replace(c, ' ');
            }
        }
        String excSpecCharStr = opStr.replace(" ", "");
        boolean isPureNum = Pattern.compile(numberic).matcher(excSpecCharStr).matches();
        boolean isPureChar = Pattern.compile(character).matcher(excSpecCharStr).matches();
        boolean isNumAndChar = Pattern.compile(number_and_character).matcher(excSpecCharStr).matches();
        isLegal = ((isPureNum && hasSpecChar)
                || (isPureChar && hasSpecChar) || isNumAndChar && hasSpecChar) || isNumAndChar;
        return isLegal;
    }

    /******************************* Checking the password   END **********************************/

    /******************************* Main Method   START  *****************************************/

    public static void main(String[] args) {

    }

    /******************************* Main Method   END    *****************************************/
}
